package com.rbac.mapper.test;

import com.rbac.pojo.Employee;
import com.rbac.pojo.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EmployeeMapper {
    @Select("select * from employee")
    List<Employee> getList();

    @Delete("delete from employee where id = #{id}")
    void delete(Integer id);

    //如果主动输入id，需要转移到xml文件修改，进行判断
    @Insert("insert into employee (username, password, nickname, email, age, admin, dept_id) values " +
            " (#{username}, #{password}, #{nickname}, #{email}, #{age}, #{admin}, #{deptId})")
    void save(Employee employee);

    @Update("update employee set username=#{username}, password=#{password}, nickname=#{nickname}, " +
            "email=#{email}, age=#{age}, admin=#{admin}, dept_id=#{deptId} where id = #{id}")
    void update(Employee employee);

    @Select("select * from employee where id = #{id}")
    Employee getById(Integer id);

    @Select("select * from employee where nickname like  CONCAT('%', #{name}, '%')")
    List<Employee> getListLikeName(String name);

    @Select("select * from employee where username = #{username}")
    Employee getEmployeeByUsername(String username);

    @Update("update employee set admin = #{admin} where id =#{id}")
    int updateState(@Param("id")Integer id, @Param("admin")Boolean admin);

    //------------------------------------------------------------------------------
    //user_role的操作
    @Insert("insert into user_role (user_id, role_id) values (#{userId}, #{roleId})")
    void saveUserRole(@Param("userId")Integer userId, @Param("roleId")Integer roleId);

    @Delete("delete from user_role where user_id = #{userId}")
    void deleteUserRole(Integer userId);

    //动态更新或者添加多个user_role信息，
    //insert into user_role (user_id, role_id) values {<foreach>(#{roleId},#{roleIdList})</foreach>

    //TODO 查询用户拥有的角色列表
    @Select("SELECT r.id, r.name " +
            "FROM role r " +
            "JOIN user_role ur ON r.id = ur.role_id " +
            "JOIN Employee u ON ur.user_id = u.id " +
            "WHERE u.id = #{userId}")
    List<Role> findRolesByUserId(@Param("id") Integer userId);

    @Select("select role_id from user_role where user_id =#{userId}")
    List<Integer> getUserRoleList(Integer userId);
}
